package com.cardbaobao.cardbabyclient.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.api.Api;
import com.cardbaobao.cardbabyclient.base.Constant;
import com.cardbaobao.cardbabyclient.callback.ItemClickedCallback;
import com.cardbaobao.cardbabyclient.model.BankData;
import com.cardbaobao.cardbabyclient.model.result.BankResult;
import com.cardbaobao.cardbabyclient.ui.activity.base.BaseActivity;
import com.cardbaobao.cardbabyclient.ui.adapter.ChooseBankAdapter;
import com.cardbaobao.cardbabyclient.utils.JsonUtil;
import com.cardbaobao.cardbabyclient.utils.ToastUtil;
import com.cardbaobao.cardbabyclient.widget.TitleBar;
import com.cardbaobao.cardbabyclient.widget.divider.VerticalItemDecoration;
import com.scwang.smartrefresh.layout.util.DensityUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import rx.Subscriber;

/**
 * Created by Jiangzx on 2017/9/12 13:45.
 */

public class ChooseBankActivity extends BaseActivity {
    public static final String CARD_TYPE_I="cardType";
    public static final String CARD_TYPE_ID_S="cardTypeId";
    public static final String CARD_NAME_S="cardName";
    public static final String CARD_LOGO_S="cardLogo";
    public static final int TYPE_DEBIT_CARD=1;//借记卡[储蓄卡]
    public static final int TYPE_CREDIT_CARD=2;//信用卡

    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    int type;
    private List<BankData.Bank> mData;
    private ChooseBankAdapter adapter;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_choose_bank;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        titleBar.setLeftViewOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handleBack();
            }
        });
    }

    private void handleBack(){
        setResult(RESULT_CANCELED,getIntent());
        finish();
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mData=new ArrayList<>();
        type=getIntent().getIntExtra(CARD_TYPE_I,TYPE_DEBIT_CARD);
        adapter=new ChooseBankAdapter(ChooseBankActivity.this,mData);
        adapter.setItemClickedCallback(new ItemClickedCallback<BankData.Bank>() {
            @Override
            public void onItemClicked(BankData.Bank bank) {
                Intent intent=getIntent();
                intent.putExtra(CARD_TYPE_ID_S,bank.getId());
                intent.putExtra(CARD_NAME_S,bank.getName());
                intent.putExtra(CARD_LOGO_S,bank.getPicture());
                setResult(RESULT_OK,intent);
                finish();
            }
        });
        LinearLayoutManager layoutManager=
                new LinearLayoutManager(ChooseBankActivity.this,
                        LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(layoutManager);
        VerticalItemDecoration decoration=new VerticalItemDecoration(Constant.DIVIDER_COLOR,1);
        int padding= DensityUtil.dp2px(20);
        decoration.setPaddingLeftRight(padding,padding);
        recyclerView.addItemDecoration(decoration);
        recyclerView.setAdapter(adapter);

        showProgressDialog(false);
        Api.getBankList(type).subscribe(new Subscriber<String>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable throwable) {
                dismissProgressDialog();
                ToastUtil.showToast("获取银行列表失败，请稍后重试");
            }

            @Override
            public void onNext(String s) {
                dismissProgressDialog();
                BankResult result= JsonUtil.json2Object(s,BankResult.class);
                if(result.getCode()==Constant.SUC_CODE){
                    BankData data=result.getData();
                    if(data!=null){
                        List<BankData.Bank> temp= data.getBankList();
                        if(temp!=null&&temp.size()>0){
                            mData.clear();
                            mData.addAll(temp);
                            adapter.notifyDataSetChanged();
                        }
                    }
                }
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK&&event.getAction()==KeyEvent.ACTION_DOWN){
            handleBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
